﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DBQLTV;

namespace DAO
{
    public class PhieuTraSachDAO
    {
        public int InsertPhieuTraSach(int TienPhatKiNay, int TienNoKyNay, int MaDocGia, string NgayTra)
        {
            using (DBQUANLYTHUVIENEntities entity = new DBQUANLYTHUVIENEntities())
            {
                string ShortDateString = Convert.ToDateTime(NgayTra).ToShortDateString();
                DateTime ngayTra = Convert.ToDateTime(ShortDateString);
                PHIEUTRASACH phieuTra = new PHIEUTRASACH()
                {
                    TienNoKiNay = TienNoKyNay,
                    TienPhatKiNay = TienPhatKiNay,
                    NgayTra = ngayTra,
                    MaDG = MaDocGia,
                };
                entity.PHIEUTRASACHes.AddObject(phieuTra);
                entity.SaveChanges();
                return phieuTra.MaPhieuTra;
            }
        }      
        /// <summary>
        /// </summary>
        /// <param name="MaDG"></param>
        /// <returns>List Mã phiếu trả sách đã xáo</returns>
        public List<int> DeletePhieuTraSach(int MaDG)  
        {
            using (DBQUANLYTHUVIENEntities entity = new DBQUANLYTHUVIENEntities())
            {
                List<int> result=new List<int>();
                var listPhieu = from temp in entity.PHIEUTRASACHes where temp.MaDG == MaDG select temp;
                if (listPhieu.Count() == 0) return null;
                foreach (PHIEUTRASACH p in listPhieu)
                {
                    result.Add(p.MaPhieuTra);
                    entity.PHIEUTRASACHes.DeleteObject(p);  
                }
                entity.SaveChanges();
                return result;
            }
        }
        public List<int> GetListPhieuTraSach(int MaDG)
        {
            using (DBQUANLYTHUVIENEntities entity = new DBQUANLYTHUVIENEntities())
            {
                 List<int> result=new List<int>();
                var listPhieu = from temp in entity.PHIEUTRASACHes where temp.MaDG == MaDG select temp;
                if (listPhieu.Count() == 0) return null;
                foreach (PHIEUTRASACH p in listPhieu)
                {
                    result.Add(p.MaPhieuTra);
                }
                entity.SaveChanges();
                return result;
            }
        }
        public PHIEUTRASACH GetPhieuTraSach(int MaPTS)
        {
            using (DBQUANLYTHUVIENEntities entity = new DBQUANLYTHUVIENEntities())
            {
                return entity.PHIEUTRASACHes.FirstOrDefault(o => o.MaPhieuTra == MaPTS);
            }
        }
    }
}
